import 'package:flutter/material.dart';
import 'package:local_auth/local_auth.dart';
import 'package:local_auth_android/local_auth_android.dart';
import 'package:local_auth_ios/local_auth_ios.dart';

class Biometrics extends StatefulWidget {
  const Biometrics({super.key});

  @override
  State<Biometrics> createState() => _BiometricsState();
}

class _BiometricsState extends State<Biometrics> {
  LocalAuthentication localAuth = LocalAuthentication();

  @override
  void dispose() {
    // TODO: implement dispose
    super.dispose();
    localAuth.stopAuthentication();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          title: const Text("指纹识别认证演示"),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ElevatedButton(
                child: const Text("检查是否支持生物识别"),
                onPressed: () async {
                  try {
                    bool canCheckBiometrics =
                        await localAuth.canCheckBiometrics;
                    print("是否支持生物识别...：${canCheckBiometrics}");
                  } catch (e) {
                    print(e);
                  }
                },
              ),
              const SizedBox(height: 30),
              ElevatedButton(
                child: const Text("获取生物识别技术列表"),
                onPressed: () async {
                  try {
                    List<BiometricType> availableBiometrics =
                        await localAuth.getAvailableBiometrics();
                    print("支持生物识别列表...：${availableBiometrics}");
                  } catch (e) {
                    print(e);
                  }
                },
              ),
              SizedBox(height: 30),
              ElevatedButton(
                child: Text("指纹/生物识别"),
                onPressed: () async {
                  try {
                    bool didAuthenticate = await localAuth.authenticate(
                        localizedReason: '扫描指纹进行身份识别',
                        authMessages: const <AuthMessages>[
                          AndroidAuthMessages(
                            cancelButton: '取消',
                            goToSettingsButton: '去设置',
                            biometricNotRecognized: '指纹识别失败',
                            goToSettingsDescription: '请设置指纹.',
                            biometricHint: '指纹',
                            biometricSuccess: '指纹识别成功',
                            signInTitle: '指纹验证',
                            deviceCredentialsRequiredTitle: '请先录入指纹!',
                          ),
                          IOSAuthMessages(
                            //更多ios的参数请参考源码
                            cancelButton: '取消',
                            goToSettingsButton: '去设置',
                            goToSettingsDescription: '请设置指纹.',
                          ),
                        ],
                        options: const AuthenticationOptions(
                            useErrorDialogs: false, stickyAuth: true));
                    print(didAuthenticate);
                  } catch (e) {
                    print(e);
                  }
                },
              )
            ],
          ),
        ));
  }
}
